package com.zh.admin.order.controller;

import com.zh.admin.order.service.OrderAdminService;
import com.zh.pojo.Orderitem;
import com.zh.pojo.Orders;
import com.zh.pojo.Products;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/admin/orderAdmin")
public class OrderAdminController {

    @Autowired
    private OrderAdminService adminService;

    @RequestMapping("/queryAllOrders")
    public String queryAllOrders(Model model){
        List<Orders> orders = adminService.selectAllOrders();
//        System.out.println("queryAllOrders --- " + orders);
        model.addAttribute("orders",orders);
        return "admin/orders/list";
    }

    @RequestMapping("/queryOrderById")
    public String queryOrderById(String id,Model model){
//        System.out.println("queryOrderById -- " + id);
        Orders order = adminService.selectOrdersById(id);
        List<Products> products = adminService.selectOrderAllProducts(id);
        System.out.println(order);
        System.out.println(products);
        model.addAttribute("order",order);
        model.addAttribute("products",products);
        return "admin/orders/view";
    }

    @RequestMapping("addOrder")
    public String addOrder(Orders orders){
//        System.out.println("addOrder --- " + orders);
        int i = adminService.insertOrder(orders);
        return "redirect:queryAllOrders";
    }

    /**
     * 删除订单
     * @param id
     * @return
     */
    @RequestMapping("removeOrderById")
    public String deleteOrderById(String id){
//        System.out.println("deleteOrderById -- " + id);
        int i = adminService.deleteOrderById(id);
//        int count = adminService.deleteOrdersItemByOrderId(id);
//        System.out.println(count);
        return "redirect:queryAllOrders";
    }

    /**
     * 条件查询
     * @param id
     * @param receiverName
     * @param model
     * @return
     */
    @RequestMapping("queryMoreOrders")
    public String queryMoreOrders(String id,String receiverName,Model model){
        if(receiverName.length() == 0) receiverName = null;
//        System.out.println("queryMoreOrders -- " + id + " -- " + receiverName);
        List<Orders> orders = adminService.selectMoreOrders(id, receiverName);
//        System.out.println("queryMoreOrders -- " + orders);
        model.addAttribute("orders",orders);
        model.addAttribute("id",id);
        model.addAttribute("receiverName",receiverName);
        return "admin/orders/list";
    }


}
